import json

from flask import Flask, request, jsonify
from flask_cors import CORS
import pymysql
from pymysql.cursors import DictCursor
import traceback

app = Flask(__name__)
CORS(app)

# 数据库配置
DB_CONFIG = {
    'host': 'localhost',
    'user': 'root',
    'password': '4014',
    'database': 'lqb',
    'charset': 'utf8mb4',
    'cursorclass': DictCursor
}


@app.route('/list')
def get_list():
    try:
        # 尝试连接数据库
        conn = pymysql.connect(**DB_CONFIG)
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM Face')
        data = cursor.fetchall()
        conn.close()

        # 返回JSON格式的响应
        return jsonify({'data': data, 'success': True})
    except Exception as e:
        # 处理异常并返回错误信息
        return jsonify({'error': str(e), 'success': False}), 500


if __name__ == '__main__':
    app.run(debug=True, port=5004)